DIVISOR = 1000000007

memo_combinations = {}
def get_combinations(n, k):
  comb = memo_combinations.get((n, k)) 
  if comb:
    return comb
  if k == 0 or k == n:
    comb = 1
  else:
    comb = (get_combinations(n-1, k) + get_combinations(n-1, k-1)) % DIVISOR
  memo_combinations[(n, k)] = comb
  return comb 

def get_for_one_dimension(x, d):
  v = [0] + [1] * d + [0]
  paths_by_number_steps = [1]
  for _ in range(m):
    v = [0] + [(v[i-1] + v[i+1]) % DIVISOR
               for i in range(1, d+1)] + [0]
    paths_by_number_steps.append(v[x])
  return paths_by_number_steps


def get_for_multi_dimension(n, x, d):
  x0 = x.pop()
  d0 = d.pop()
  last_dimension = get_for_one_dimension(x0, d0)
  if n == 1:  
    return last_dimension
  

  without_last_dimension = get_for_multi_dimension(n-1, x, d)
  paths_by_number_steps = [0] * (m+1)
  for m1 in range(m+1):
    for i in range(m1+1):
      paths_by_number_steps[m1] += (without_last_dimension[i] * last_dimension[m1-i] * get_combinations(m1, i)) % DIVISOR
    paths_by_number_steps[m1] %= DIVISOR
  return paths_by_number_steps

import sys
def wrap_input():
  return sys.stdin.readline()

t = int(wrap_input())
for _ in range(t):
  n, m = map(int, wrap_input().split(' '))
  x = list(map(int, wrap_input().split(' ')))  
  d = list(map(int, wrap_input().split(' ')))
  print(get_for_multi_dimension(n, x, d)[-1])

